local blaze = require "blaze"
local app = require "app"
local game = app.game.running
local consts = game.util.consts
local coroutine = game.gamelib.coroutine

local M = game.ui.element()

M.store_watchers = {
    gameplay = {
        ["$"] = blaze.noop,


        ["展示结果"] = function(self, store, state, args)
            self:play(state.result_view.banker, state.result_view.player, state.result_view.banker_value, state.result_view.player_value);
        end,

        ["阶段变更"] = function(self, store, state, args)
            if state.phase_id == consts.PHASE_IDLE 
            or state.phase_id == consts.PHASE_BET
            or state.phase_id == consts.PHASE_SETTLE
            or state.phase_id == consts.PHASE_PAYOUT then
                self:hide()
            end
        end,

        
    }
}

function M:ctor(bet_area)
    self.res = "club.threecard.baccarat/ui/ingame.b:result_shower"
    self.bet_area = bet_area

    local poker_container = game.pokerlib.core.poker_container
    local config = game.require "poker_controller.poker_container_config"
    self.banker_container = poker_container.new(config)
    self.player_container = poker_container.new(config)
end

function M:on_load()
    self:super("on_load")

    self.banker_container:load()
    self.banker_container:set_parent(self.banker_root, false)

    self.player_container:load()
    self.player_container:set_parent(self.player_root, false)
end

local function get_value(poker_container)
    local value = 0
    for _, poker in pairs(poker_container.hand) do
        local client_value = poker.data.client_value
        if client_value >= 10 then
            client_value = 0
        end
        value = value + client_value
    end

    value = value % 10
    return value
end

local function update_player_view(self)
    self.player_value_txt.text = get_value(self.player_container)
end

local function update_banker_view(self)
    self.banker_value_txt.text = get_value(self.banker_container)
end

local function mopai(poker_container, camp_name, card_ids, txt)
    poker_container:mopai(card_ids)
    game.audio.post("baccarat:"..camp_name)
    coroutine.wait(0.4)
    txt.text = get_value(poker_container)
    coroutine.wait(0.4)
end

local function report_result(camp_name, value)
    game.audio.post("baccarat:"..camp_name)
    coroutine.wait(0.45)
    game.audio.post("baccarat:p"..value)
    coroutine.wait(0.9)
end

function M:play(banker, player, banker_value, player_value)
    if not self.loaded then
        return
    end

    self.banker_container:clear()
    self.player_container:clear()

    self:start_coroutine(function()
        self:show()
        self.result_obj:SetActive(false)
        self.player_value_txt.text = '0'
        self.banker_value_txt.text = '0'
        self.animator:Play("result_show_clip")
        coroutine.wait(0.25)
        
        mopai(self.player_container, "player", {player[1]}, self.player_value_txt)
        mopai(self.banker_container, "banker", {banker[1]}, self.banker_value_txt)
        mopai(self.player_container, "player", {player[2]}, self.player_value_txt)
        mopai(self.banker_container, "banker", {banker[2]}, self.banker_value_txt)

        if player[3] then
            mopai(self.player_container, "onemore_poker_player", {player[3]}, self.player_value_txt)
        end
        if banker[3] then
            mopai(self.banker_container, "onemore_poker_banker", {banker[3]}, self.banker_value_txt)
        end

        coroutine.wait(0.4)

        report_result("player", player_value)
        report_result("banker", banker_value)

        self.result_banker:SetActive(banker_value > player_value)
        self.result_tie:SetActive(banker_value == player_value)
        self.result_player:SetActive(banker_value < player_value)
        self.result_obj:SetActive(true)

        coroutine.wait(0.1)
        if banker_value == player_value then
            game.audio.post("baccarat:result_tie_win")
        elseif banker_value < player_value then
            game.audio.post("baccarat:result_player_win")
        else
            game.audio.post("baccarat:result_banker_win")
        end

        self.co = nil
    end)
end

function M:hide()
    if not self.loaded then
        return
    end

    self.animator:Play("result_hide_clip")
end

function M:on_release()
    self:super("on_release")
    self.banker_container:release()
    self.player_container:release()
end

return M